// components/comment/comment.js
import { giveLikeApi } from '../../utils/tools.js';
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        commentList: Array,
        typeLike: String,
        identityArr: Array,
        otherPage: String,
        commentType: String,
    },

    /**
     * 组件的初始数据
     */
    data: {
        likeStatus: true,
        commentLists: Array,
    },
    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () {},
    },
    observers: {
        commentList: function (commentList) {
            for (let i = 0; i < commentList.length; i++) {
                commentList[i].createtime = app.timeago(
                    commentList[i].createtime
                );
                commentList[i].hasPermission = this.hasPermission(
                    commentList[i].createuid
                );
                commentList[i].canReply =
                    commentList[i].createuid !== app.globalData.uid;
                if (commentList[i].replyList) {
                    for (let j = 0; j < commentList[i].replyList.length; j++) {
                        commentList[i].replyList[j].createtime = app.timeago(
                            commentList[i].replyList[j].createtime
                        );
                        commentList[i].replyList[
                            j
                        ].hasPermission = this.hasPermission(
                            commentList[i].replyList[j].createuid
                        );
                        commentList[i].replyList[j].canReply =
                            commentList[i].replyList[j].createuid !==
                            app.globalData.uid;
                    }
                }
            }
            console.log(commentList);
            this.setData({
                commentLists: commentList,
            });
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        hasPermission(uid) {
            let hasPermission =
                this.data.identityArr.includes(app.globalData.authority) ||
                app.globalData.uid === uid;
            return hasPermission;
        },
        // 点击评论
        goToReply(e) {
            if (!e.currentTarget.dataset.canReply) return;
            let _that = this;
            _that.triggerEvent('remarkInfo', {
                battleTag: e.currentTarget.dataset.battleTag,
                contentid: e.currentTarget.dataset.contentid,
                anonymousflg: e.currentTarget.dataset.anonymousflg,
                privateflg: e.currentTarget.dataset.privateflg,
                touid: e.currentTarget.dataset.touid,
            });
        },

        goToSecondaryReply(e) {
            if (!e.currentTarget.dataset.canReply) return;
            this.triggerEvent('remarkInfo', {
                battleTag: e.currentTarget.dataset.battleTag,
                tocommentid: e.currentTarget.dataset.tocommentid,
                anonymousflg: e.currentTarget.dataset.anonymousflg,
                privateflg: e.currentTarget.dataset.privateflg,
                touid: e.currentTarget.dataset.touid,
                eventid: e.currentTarget.dataset.eventid,
                toreplyid: e.currentTarget.dataset.toreplyid,
            });
        },

        //点赞
        clickLike(e) {
            let _that = this;
            console.log(e.currentTarget.dataset.indexreply);
            let cntName;
            let flgName;
            let cnt = e.currentTarget.dataset.cnt;
            let flg = e.currentTarget.dataset.flg;
            if (
                e.currentTarget.dataset.indexreply == '0' ||
                e.currentTarget.dataset.indexreply
            ) {
                cntName =
                    'commentLists[' +
                    e.currentTarget.dataset.index +
                    '].replyList[' +
                    e.currentTarget.dataset.indexreply +
                    '].praisecnt';
                flgName =
                    'commentLists[' +
                    e.currentTarget.dataset.index +
                    '].replyList[' +
                    e.currentTarget.dataset.indexreply +
                    '].praiseflg';
            } else {
                cntName =
                    'commentLists[' +
                    e.currentTarget.dataset.index +
                    '].praisecnt';
                flgName =
                    'commentLists[' +
                    e.currentTarget.dataset.index +
                    '].praiseflg';
            }
            _that.setData({
                [cntName]: flg == '00' ? cnt + 1 : cnt - 1,
                [flgName]: flg == '00' ? '01' : '00',
            });
            let praiseflg =
                e.currentTarget.dataset.indexreply == '0' ||
                e.currentTarget.dataset.indexreply
                    ? _that.data.commentLists[e.currentTarget.dataset.index]
                          .replyList[e.currentTarget.dataset.indexreply]
                          .praiseflg
                    : _that.data.commentLists[e.currentTarget.dataset.index]
                          .praiseflg;
            giveLikeApi(
                _that.data.typeLike,
                e.currentTarget.dataset.id,
                praiseflg
            );
        },
        deleteRemark(e) {
            this.triggerEvent('deleteRemark', {
                id: e.currentTarget.dataset.id,
                createuid: e.currentTarget.dataset.uid,
            });
        },
        // 查看他人主页
        jumpOthersPage(e) {
            if (e.currentTarget.dataset.anonymousflg == '00') {
                wx.navigateTo({
                    url: `/functionPage/pages/othersHomePage/othersHomePage?uid=${e.currentTarget.dataset.uid}`,
                });
            }
        },
    },
});
